Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class yandex_photo{
- private $rsa_key = null;
- private $request_id = null;
- private $token = null;
- private $login = null;
- private $password = null;
- private $last_message = null;
- public function __construct($login=null,$password=null){
- if($login==null||$password==null){
- throw new Exception("Не заданы данные аутентификации",E_ERROR);
- }
- $this->login = $login;
- $this->password = $password;
- }
- public function encrypt_yarsa2($key, $data){
- $buffer = array();
- list($nstr, $estr) = explode('#', $key);
- $n = gmp_init('0x' .$nstr);
- $e = gmp_init($estr);
- $stepSize = strlen($nstr) / 2 - 1;
- $prev_crypted = array();
- $prev_crypted = array_fill(0, $stepSize, 0);
- $hex_out = '';
- for($i = 0; $i < strlen($data); ++$i)
- {
- $buffer[$i] = ord($data{$i});
- }
- for ($i = 0; $i < intval((count($buffer) - 1) / $stepSize) + 1; ++$i)
- {
- $tmp = array_slice($buffer, $i * $stepSize, ($i + 1) * $stepSize);
- for ($j = 0; $j < count($tmp); ++$j)
- {
- $tmp[$j] = ($tmp[$j] ^ $prev_crypted[$j]);
- }
- $tmp = array_reverse($tmp);
- $plain = gmp_init(0);
- for($x = 0; $x < count($tmp); ++$x)
- {
- $pow = gmp_powm(gmp_init(256), gmp_init($x), $n);
- $pow_mult = gmp_mul($pow, gmp_init($tmp[$x]));
- $plain = gmp_add($plain, $pow_mult);
- }
- $plain_pow = gmp_powm($plain, $e, $n);
- $plain_pow_str = gmp_strval($plain_pow, 16);
- $hex_result = array_fill(0, array(strlen($nstr) - strlen($plain_pow_str)), '0');
- $hex_result = implode($hex_result) + $plain_pow_str;
- $min_x = min(strlen($hex_result), count($prev_crypted) * 2);
- for ($x = 0; $x < $min_x; $x = $x + 2)
- {
- $prev_crypted[$x / 2] = hexdec('0x' + substr($hex_result, $x, 2));
- }
- if(count($tmp) < 16)
- {
- $hex_out += '00';
- }
- $hex_out += dechex(count($tmp)) + '00';
- $ks = strlen($nstr) / 2;
- if($ks < 16)
- {
- $hex_out += '0';
- }
- $hex_out += dechex($ks) + '00';
- $hex_out += $hex_result;
- }
- return base64_encode(pack("H*" , $hex_out));
- }
- public function encrypt_yarsa($key, $toenc){
- $data_arr = array();
- $nstr = explode("#",$key);
- $estr = $nstr[1];
- $nstr = $nstr[0];
- $n = hexdec($nstr);
- $e = hexdec($estr);
- $step_size = strlen($nstr)/2-1;
- $i=$step_size;
- while($i--){
- $prev_crypted[]="";
- }
- $hex_out = "";
- $plain = "";
- for($i=0;$i<strlen($toenc);$i++){
- $data_arr[] = ord(substr($toenc,$i,1));
- }
- for($i=0;$i<(int)((count($data_arr)-1)/$step_size)+1;$i++){
- $tmp = array_slice($data_arr,$i*$step_size,(($i+1)*$step_size) - ($i*$step_size));
- for($j=0;$j<count($tmp);$j++){
- $tmp[$j] = $tmp[$j]^$prev_crypted[$j];
- }
- $tmp = array_reverse($tmp);
- $plain = 0;
- for($x=0;$x<count($tmp);$x++){
- $pow = $this->m_powmod(256,$x,$n);
- $pow_mult = $pow*$tmp[$x];
- $plain = $plain+$pow_mult;
- }
- //Пока все совпадает
- //plain_pow = powMod(plain, str2bigInt(E,10,0), str2bigInt(N,10,0));
- $plain_pow = $this->m_powmod($plain,$e,$n);
- echo "<br/>plain: ";
- echo $plain;
- echo "<br/>e: ";
- echo $e;
- echo "<br/>n: ";
- echo $n;
- echo "<br/>plain_pow: ";
- echo $plain_pow;
- echo "<br/>";
- /*
- $plain_pow_str = (string)dechex($plain_pow);
- $hex_result = array();
- $i=$step_size;
- while($i--){
- $hex_result[]="";
- }
- $hex_result = implode("0",$hex_result).$plain_pow_str;
- $min_x = min(strlen($hex_result), count($prev_crypted)*2);
- for($x=0;$x<$min_x;$x=$x+2){
- $prev_crypted[$x/2] = (int)("0x"+substr($hex_result,$x, 2));
- }
- if(count($tmp)<16){
- $hex_out.="00";
- }
- $hex_out.= strtoupper(dechex(count($tmp)))."00";
- $ks = strlen((string)$nstr)/2;
- if($ks<16){
- $hex_out.="0";
- }
- $hex_out.= strtoupper(dechex($ks))."00";
- $hex_out.= $hex_result;
- */
- }
- $pattern = "|[\n\r\t]+|usi";
- return preg_replace($pattern,"",base64_encode(hexdec(strtolower($hex_out))));
- }
- private function m_mod($a,$b){
- return floor($a)-floor($a/$b)*$b;
- }
- private function m_powmod($d, $p, $m){
- for ($b = 1; $p;){
- if ($p & 1){
- $p--;
- $b = $this->m_mod($b * $d, $m);
- }else{
- $p >>= 1;
- $d = $this->m_mod($d * $d, $m);
- }
- }
- return $b;
- }
- public function authentication(){
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, "http://auth.mobile.yandex.ru/yamrsa/key/");
- curl_setopt($curl, CURLOPT_HEADER, false);
- curl_setopt($curl, CURLOPT_HTTPGET, true);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- echo $this->last_message = curl_exec($curl);
- curl_close($curl);
- }
- }
- /*
- <?xml version="1.0"?>
- <response>
- <key>962F27B863D09CC600374CD7705F4DA134C1B69400DCB077E528B20F388AA8C427DD7D26BFA20AC55F7EEAC45F826593D411CDF7746E6E097E2D1CB1EB3B38E9#10001</key>
- <request_id>cbb9ad4f3485425bb7e95df46fb1ea51</request_id>
- </response>
- */
- header('content-type:text/html; charset=utf-8');
- $photo = new yandex_photo("silentimp","fkytnyjfy_2002");
- //$photo->authentication();
- echo $photo->encrypt_yarsa("962F27B863D09CC600374CD7705F4DA134C1B69400DCB077E528B20F388AA8C427DD7D26BFA20AC55F7EEAC45F826593D411CDF7746E6E097E2D1CB1EB3B38E9#10001",'<credentials login="mojo" password="qwerty"/>');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment